package com.zy.explore.module.activity.interfaces.assembler;

import com.zy.explore.common.infrastructure.util.DataBeanUtil;
import com.zy.explore.common.interfaces.frm.CommonIdFrm;
import com.zy.explore.common.interfaces.frm.CommonIdsFrm;
import com.zy.explore.common.interfaces.vo.PageVO;
import com.zy.explore.module.activity.application.service.MybatisExampleService;
import com.zy.explore.module.activity.domain.mybatis.entity.MybatisExampleEntity;
import com.zy.explore.module.activity.interfaces.frm.ExampleSaveFrm;
import com.zy.explore.module.activity.interfaces.frm.ExampleQueryFrm;
import com.zy.explore.module.activity.interfaces.vo.ExampleVO;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import java.util.stream.Collectors;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

/**
 * description Mybatis示例Assembler
 *
 * @author ZY
 * @date 2021年05月15日 11:23
 */
@Component
public class MybatisExampleAssembler {

    /**
     * 注入service
     */
    @Resource
    private MybatisExampleService service;

    /**
     * 新增数据
     *
     * @param frm 参数对象
     * @return 视图对象
     */
    public ExampleVO insert(ExampleSaveFrm frm) {
        MybatisExampleEntity entity = convertFromFrm(frm);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return convertToVO(service.insertSelective(entity));
    }

    /**
     * 删除对象
     *
     * @param frm 参数对象
     * @return true:删除成功 false:删除失败
     */
    public Integer delete(CommonIdFrm frm) {
        return service.deleteByPrimaryKey(Integer.parseInt(frm.getId()));
    }

    /**
     * 更新对象
     *
     * @param frm 待更新的Frm对象
     * @return 成功返回更新后的VO对象，失败返回null
     */
    public ExampleVO update(ExampleSaveFrm frm) {
        MybatisExampleEntity entity = convertFromFrm(frm);
        entity.setUpdateTime(new Date());
        int res = service.updateByPrimaryKeySelective(entity);
        if (res == 1) {
            return convertToVO(entity);
        }
        return null;
    }

    /**
     * 根据ID查询数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的VO对象，失败返回null
     */
    public ExampleVO findById(CommonIdFrm frm) {
        return convertToVO(service.getByPrimaryKey(Integer.parseInt(frm.getId())));
    }

    /**
     * 通过主键批量查询数据集合
     *
     * @param frm 主键集合Frm对象
     * @return 成功返回对应ID集合的VO对象集合，失败返回空集合
     */
     public List<ExampleVO> findByIds(CommonIdsFrm frm) {
        List<ExampleVO> retList = new ArrayList<>();
        if (CollectionUtils.isEmpty(frm.getIds())) {
            return retList;
        }
        List<MybatisExampleEntity> list = service.getByPrimaryKeys(frm.getIds().stream().map(Integer::parseInt).collect(Collectors.toList()));
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(convertToVO(x)));
        }
        return retList;
     }

    /**
     * 根据条件查询数据集合
     *
     * @param frm 查询条件对象
     * @return 满足条件的数据集合
     */
    public List<ExampleVO> getList(ExampleQueryFrm frm) {
        List<ExampleVO> retList = new ArrayList<>();
        Example example = MybatisExampleEntity.getExample(frm);
        List<MybatisExampleEntity> list = service.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(x -> retList.add(convertToVO(x)));
            return retList;
        }
        return retList;
    }

    /**
     * 根据条件查询分页
     *
     * @param frm 查询条件
     * @return 满足条件的分页
     */
    public PageVO<ExampleVO> getPageInfo(ExampleQueryFrm frm) {
        Example example = MybatisExampleEntity.getExample(frm);
        PageInfo<MybatisExampleEntity> pageInfo = PageHelper.startPage(frm.getPageNum(), frm.getPageSize()).doSelectPageInfo(() -> service.selectByExample(example));
        PageRequest pageRequest = PageRequest.of(frm.getPageNum() - 1, frm.getPageSize());
        List<ExampleVO> retList = new ArrayList<>();
        Page<ExampleVO> retPage = new PageImpl<>(retList, pageRequest, 0);
        if (pageInfo == null || CollectionUtils.isEmpty(pageInfo.getList())) {
            return new PageVO<>(retPage);
        }

        pageInfo.getList().forEach(x -> retList.add(convertToVO(x)));
        retPage = new PageImpl<>(retList, pageRequest, pageInfo.getTotal());
        return new PageVO<>(retPage);
    }

    /**
     * Frm 转 实体类
     *
     * @param frm 参数对象
     * @return 返回转换后的实体对象
     */
    public static MybatisExampleEntity convertFromFrm(ExampleSaveFrm frm) {
        MybatisExampleEntity entity = DataBeanUtil.copyOf(frm, MybatisExampleEntity.class, true);
        if (!StringUtils.isEmpty(frm.getId())) {
            entity.setId(Integer.parseInt(frm.getId()));
        }
        return entity;
    }

    /**
     * 实体类 转 VO
     *
     * @param entity 实体类对象
     * @return 返回转化后的VO对象
     */
    public static ExampleVO convertToVO(MybatisExampleEntity entity) {
        if (entity == null) {
            return null;
        }
        ExampleVO vo = DataBeanUtil.copyOf(entity, ExampleVO.class, true);
        vo.setId(entity.getId().toString());
        return vo;
    }

}
